<?php

/**
 * 목록 페이지번호를 반환한다.
 *
 * @return int $pageno 페이지번호
 */
function esample_admin_get_pageno() {
    if (isset($_GET["pageno"]) && $_GET["pageno"]) {
        return $_GET["pageno"];
    }
    return 1;
}

/**
 * 검색 where 절을 반환한다.
 *
 * @return string where절
 */
function esample_admin_get_where() {
    if (isset($_GET["sv"]) && $_GET["sv"]) {
        return " WHERE es_name LIKE '%" . $_GET["sv"] . "%' ";
    }
    return "";
}

/**
 * order by 절을 반환한다.
 *
 * @author cwsong
 * @since 0.3
 * @return string order by 절
 */
function esample_admin_get_order() {
    if (isset($_GET["col"]) && $_GET["col"] && isset($_GET["direction"]) && $_GET["direction"]) {
        return " ORDER BY " . $_GET["col"] . " " . $_GET["direction"] . " ";
    }
    return " ORDER BY es_no DESC";
}

/**
 * 추가파라메터 설정
 *
 * @author cwsong
 * @since 0.3
 * @return string 추가파라메터
 */
function esample_admin_get_extra_params() {
    $str = "";

    if (isset($_GET["sv"]) && $_GET["sv"]) {
        $str.= "page=".EPART_PLUGIN_NAME."-admin&sv=" . $_GET["sv"];
    } else {
        $str .= "page=".EPART_PLUGIN_NAME."-admin";
    }

    if (isset($_GET["col"]) && $_GET["col"] && isset($_GET["direction"]) && $_GET["direction"]) {
        $str .= "&col=" . $_GET["col"] . "&direction=" . $_GET["direction"];
    }

    return $str;
}

/**
 * 정렬 링크를 반환한다.
 *
 * @author cwsong
 * @since 0.3
 * @param string $col 정렬기준 컬럼명
 * @return 정렬링크 파라메터 문자열
 */
function esample_admin_get_order_link($col = null) {
    if (is_null($col)) {
        wp_die("정렬링크 생성에 필요한 인자값이 바르게 입력되지 않았습니다.");
    }
    $str = "";
    if (isset($_GET["direction"]) && $_GET["direction"] == "desc") {
        $str .= "&col=" . $col . "&direction=asc";
    } else {
        $str .= "&col=" . $col . "&direction=desc";
    }

    if (isset($_GET["pageno"]) && $_GET["pageno"]) {
        $str .= "&pageno=" . $_GET["pageno"];
    }
    return $str;
}

/**
 * 게시판 정렬 삼각형을 반환한다.
 *
 * @author cwsong
 * @since 0.3
 * @param string $col 정렬기준 컬럼명
 * @return 정렬링크 옆에 나올 삼각형
 */
function get_triangle($col) {
    if (isset($_GET["col"]) && $_GET["col"] && isset($_GET["direction"]) && $_GET["direction"]) {
        if ($_GET["col"] == $col) {
            return $_GET["direction"] == "desc" ? " ▼ " : " ▲ ";
        }
    }
    return "";
}

//페이징 오브젝트 생성
$paging = new EpartPaging("wp_".EPART_PLUGIN_NAME, esample_admin_get_pageno(), 'plain', esample_admin_get_where(), esample_admin_get_order(), esample_admin_get_extra_params(),10);
//페이징오브젝트로부터 목록결과 받기
$result = $paging->getResult();
?>
<script type="text/javascript">
//<![CDATA[
jQuery(function($){
    //삭제버튼 클릭시
    $("#" + "<?php echo EPART_PLUGIN_NAME;?>" + "_list_form").submit(function(){
        if($(":checkbox:checked").length === 0){
            $(".msg-area").empty().append("<div class='updated'>삭제할 row를 선택해주세요.</d>");
        }else{
            $(".msg-area").empty();
            if(confirm("선택하신 row를 정말 삭제하시겠습니까?\n삭제하신 row는 복구할 수 없습니다.")){
                return true;
            }
        }
        return false;
    });

    //검색 - 레이아웃위치상 폼 안에 들어가 있어서 부득이하게 스크립트 처리함...아쉽다-_- - div로 쪼개서 css처리하면 되긴 하는데 --;; 나중에...
    $("#search_btn").click(function(e){
        location.href = "<?php echo preg_replace("/&sv=.*/", "", $list_url) ?>&sv=" + encodeURIComponent($.trim($("#sv").val()));
    });
    $("#sv").keydown(function(e){
        if(e.keyCode == 13){
            $("#search_btn").click();
            e.preventDefault();
        }
    });
});
//]]>
</script>
<form id="<?php echo EPART_PLUGIN_NAME;?>_list_form" action="<?php _e($delete_db_url); ?>" method="post">
    <?php wp_nonce_field(EPART_PLUGIN_NAME."-list"); ?>
    <div class="msg-area"><!-- --></div>
    <table style="width: 100%;">
        <tr>
            <td style="text-align: left;">
                <p>
                    <input type="submit" value="선택삭제" class="button-primary" />
                </p>
            </td>
            <td style="text-align: right;" class="table-foot-div description">
                <input id="sv" type="text" name="sv" value="<?echo isset($_GET["sv"]) ? $_GET["sv"] : ""; ?>" />
                <input id="search_btn" type="button" class="button" value="검색" />
            </td>
        </tr>
    </table>
    <table id="<?php echo EPART_PLUGIN_NAME;?>_Table" class="wp-list-table widefat fixed" cellspacing="0">
        <colgroup>
            <col width="4%" />
            <col width="11%" />
            <col width="35%" />
            <col width="20%" />            
        </colgroup>
        <thead>
            <tr>
                <th scope="col" class="manage-column"></th>
                <th scope="col" class="manage-column">
                    <a href="<?php _e($list_url.esample_admin_get_order_link("es_no")); ?>">등록번호<?php _e(get_triangle("es_no")); ?></a>
                </th>
                <th scope="col" class="manage-column">
                    <a href="<?php _e($list_url.esample_admin_get_order_link("es_name")); ?>">이름<?php _e(get_triangle("es_name")); ?></a>
                </th>
                <th scope="col" class="manage-column">
                    <a href="<?php _e($list_url.esample_admin_get_order_link("es_title")); ?>">제목<?php _e(get_triangle("es_title")); ?></a>
                </th>
                <th scope="col" class="manage-column">
                    <a href="<?php _e($list_url.esample_admin_get_order_link("es_regdate")); ?>">등록일<?php _e(get_triangle("es_regdate")); ?></a>
                </th>
            </tr>
        </thead>
        <tfoot>
             <tr>
                <th scope="col" class="manage-column"></th>
                <th scope="col" class="manage-column">
                    <a href="<?php _e($list_url.esample_admin_get_order_link("es_no")); ?>">등록번호<?php _e(get_triangle("es_no")); ?></a>
                </th>
                <th scope="col" class="manage-column">
                    <a href="<?php _e($list_url.esample_admin_get_order_link("es_name")); ?>">이름<?php _e(get_triangle("es_name")); ?></a>
                </th>
                <th scope="col" class="manage-column">
                    <a href="<?php _e($list_url.esample_admin_get_order_link("es_title")); ?>">제목<?php _e(get_triangle("es_title")); ?></a>
                </th>
                <th scope="col" class="manage-column">
                    <a href="<?php _e($list_url.esample_admin_get_order_link("es_regdate")); ?>">등록일<?php _e(get_triangle("es_regdate")); ?></a>
                </th> 
            </tr>
        </tfoot>
        <tbody>
            <?php if($paging->getTotal() <= 0):?>
            <tr>
                <td colspan="4">row가 없습니다.</td>
            </tr>
            <?php endif;?>
            <?php foreach($result as $row):?>
            <tr>
                <td><input type="checkbox" name="es_no[]" value="<?echo $row->es_no; ?>" /></td>
                <td><?php echo $row->es_no; ?></td>
                <td><a href="<?php echo $update_url; ?>&es_no=<?php echo $row->es_no; ?>"><?php echo $row->es_name; ?></a></td>
                <td><?php echo $row->es_title; ?></td>
                <td><?php echo $row->es_regdate; ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <table style="width: 100%;">
        <tr>
            <td style="text-align: left;">
                <p>
                    <input type="submit" value="선택삭제" class="button-primary" />
                </p>
            </td>
            <td style="text-align: right;">
                <span class="description">총 <?php echo $paging->getTotal();?>개의 row가 있습니다.</span>
            </td>
        </tr>
    </table>
    <div id="paging">
        <?php echo $paging->getPlainPagingTag(); ?>
    </div>
</form>
